package com.monster.security.service.impl;

import com.monster.admin.user.domain.service.SysUserAuthService;
import com.monster.admin.user.domain.vo.LoginVo;
import com.monster.security.convert.SecurityConvert;
import com.monster.security.entity.UserDetail;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

/**
 * 登录专用类
 * 自定义类，实现了UserDetailsService接口，用户登录时调用的第一类
 *
 * @author kuangxz
 */
@Slf4j
@Component
public class UserDetailsServiceImpl implements UserDetailsService {
    /**
     * 授权service
     */
    @Autowired
    private SysUserAuthService authService;
    /**
     * 数据转换
     */
    @Autowired
    private SecurityConvert securityConvert;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        LoginVo user = authService.login(username);
        UserDetail detail = securityConvert.toUserDetail(user);
        return detail;
    }
}
